package com.usernet.product.server;

import java.util.List;
import java.util.Random;

import javax.servlet.http.HttpServletRequest;

import com.usernet.product.dao.CommentDao;
import com.usernet.product.dao.ResourcesDao;
import com.usernet.product.dao.SoftwareDao;
import com.usernet.product.dao.TypeDao;
import com.usernet.product.entity.Resources;
import com.usernet.product.entity.Software;
import com.usernet.product.entity.Type;
import com.usernet.product.tools.BaseCache;
import com.usernet.product.tools.JSONTools;
import com.usernet.product.utils.DateUtils;
import com.usernet.product.utils.ProductConfig;

public class SoftwareService {

	private SoftwareDao softwareDao = new SoftwareDao();
	private TypeDao typeDao = new TypeDao();
	private CommentDao commentDao = new CommentDao();
	private ResourcesDao resourcesDao = new ResourcesDao();
	private static BaseCache otherCache = new BaseCache("resource_other",
			ProductConfig.cacheTime);// 缓存时间

	/**
	 * 精品推荐[type=3]
	 */
	public String initNew(HttpServletRequest request) {
		String ret = null;
		try {
			int flag = Integer.parseInt(request.getParameter("flag"));
			int size = Integer.parseInt(request.getParameter("pageSize"));
			int no = Integer.parseInt(request.getParameter("pageNo"));

			List<Software> list = null;
			int maxSize = 0;
			switch (flag) {
			case 1:
				// 精品软件
				list = softwareDao.getAllTops(size, no);
				if (no == 1) {
					maxSize = softwareDao.getAllTopsCount();
				}
				break;
			case 2:
				// 编辑推荐
				list = softwareDao.getAllRecommend(size, no);
				if (no == 1) {
					maxSize = softwareDao.getAllRecommendCount();
				}
				break;
			case 3:
				// 最新上架
				list = softwareDao.getAllHot(size, no);
				if (no == 1) {
					maxSize = softwareDao.getAllHotCount();
				}
				break;
			default:
				break;
			}

			StringBuffer buf = new StringBuffer();
			buf.append("[");
			if (list != null) {
				for (int i = 0; i < list.size(); i++) {
					Software software = list.get(i);
					buf.append("{");
					buf.append("id:");
					buf.append(software.getId());
					buf.append(",name:\"");
					buf.append(JSONTools.string2Json(software.getName()));
					buf.append("\",icon:\"");
					buf.append(software.getIcon());
					buf.append("\",develope:\"");
					buf.append(JSONTools.string2Json(software.getDevelope()));
					buf.append("\",avg:");
					buf.append(software.getAvg());
					buf.append("}");
					if (i < list.size() - 1) {
						buf.append(",");
					}
				}
			}
			buf.append("],size:");
			buf.append(maxSize);
			ret = MarketUtils.getResJson(buf.toString());
		} catch (Exception ex) {
			ex.printStackTrace();
			ret = MarketUtils.getThrowJson();
		}
		return ret;
	}

	/**
	 * 软件排名[type=5]
	 */
	public String initSort(HttpServletRequest request) {
		String ret = null;
		try {
			int size = Integer.parseInt(request.getParameter("pageSize"));
			int no = Integer.parseInt(request.getParameter("pageNo"));
			int s = Integer.parseInt(request.getParameter("sort"));
			List<Software> list = null;
			int maxSize = 0;
			list = softwareDao.getAllSort(size, no, s);
			if (no == 1) {
				maxSize = softwareDao.getSortCount();
			}
			StringBuffer buf = new StringBuffer();
			buf.append("[");
			for (int i = 0; i < list.size(); i++) {
				Software software = list.get(i);
				buf.append("{");
				buf.append("id:");
				buf.append(software.getId());
				buf.append(",name:\"");
				buf.append(JSONTools.string2Json(software.getName()));
				buf.append("\",icon:\"");
				buf.append(software.getIcon());
				buf.append("\",develope:\"");
				buf.append(JSONTools.string2Json(software.getDevelope()));
				buf.append("\",avg:");
				buf.append(software.getAvg());
				buf.append("}");
				if (i < list.size() - 1) {
					buf.append(",");
				}
			}
			buf.append("],size:");
			buf.append(maxSize);
			ret = MarketUtils.getResJson(buf.toString());
		} catch (Exception ex) {
			ex.printStackTrace();
			ret = MarketUtils.getThrowJson();
		}
		return ret;
	}

	/**
	 * 分类信息[type=6]
	 */
	@SuppressWarnings("unchecked")
	public String initType(HttpServletRequest request) {
		String ret = null;
		try {
			int size = Integer.parseInt(request.getParameter("pageSize"));
			int no = Integer.parseInt(request.getParameter("pageNo"));
			int flag = Integer.parseInt(request.getParameter("flag"));
			List<Type> list = (List<Type>) otherCache.get("software_type_list_"
					+ size + "_" + no + "_" + flag);
			Integer maxSize = (Integer) otherCache.get("software_type_size_"
					+ size + "_" + no + "_" + flag);
			if (list == null || maxSize == null) {
				list = typeDao.getAllType(size, no, flag);
				if (no == 1) {
					maxSize = typeDao.getTypeCount(flag);
				}
				otherCache.put("software_type_list_" + size + "_" + no + "_"
						+ flag, list);
				otherCache.put("software_type_size_" + size + "_" + no + "_"
						+ flag, maxSize);
			}
			StringBuffer buf = new StringBuffer();
			buf.append("[");
			for (int i = 0; i < list.size(); i++) {
				Type type = list.get(i);
				buf.append("{");
				buf.append("id:");
				buf.append(type.getId());
				buf.append(",name:\"");
				buf.append(JSONTools.string2Json(type.getName()));
				buf.append("\",icon:\"");
				buf.append(type.getIcon());
				buf.append("\",remark:\"");
				buf.append(JSONTools.string2Json(type.getRemark()));
				buf.append("\"}");
				if (i < list.size() - 1) {
					buf.append(",");
				}
			}
			buf.append("],size:");
			buf.append(maxSize);
			ret = MarketUtils.getResJson(buf.toString());
		} catch (Exception ex) {
			ex.printStackTrace();
			ret = MarketUtils.getThrowJson();
		}
		return ret;
	}

	/**
	 * 专题信息[type=4]
	 */
	@SuppressWarnings("unchecked")
	public String initResources(HttpServletRequest request) {
		String ret = null;
		try {
			int size = Integer.parseInt(request.getParameter("pageSize"));
			int no = Integer.parseInt(request.getParameter("pageNo"));
			List<Resources> list = (List<Resources>) otherCache
					.get("software_res_list_" + size + "_" + no);
			Integer maxSize = (Integer) otherCache.get("software_res_size_"
					+ size + "_" + no);
			if (list == null || maxSize == null) {
				list = resourcesDao.getAllResources(size, no);
				if (no == 1) {
					maxSize = resourcesDao.getResourcesCount();
				}
				otherCache.put("software_res_list_" + size + "_" + no, list);
				otherCache.put("software_res_size_" + size + "_" + no, maxSize);
			}
			StringBuffer buf = new StringBuffer();
			buf.append("[");
			for (int i = 0; i < list.size(); i++) {
				Resources type = list.get(i);
				buf.append("{");
				buf.append("id:");
				buf.append(type.getId());
				buf.append(",name:\"");
				buf.append(JSONTools.string2Json(type.getName()));
				buf.append("\",icon:\"");
				buf.append(type.getIcon());
				buf.append("\",remark:\"");
				buf.append(JSONTools.string2Json(type.getRemark()));
				buf.append("\"}");
				if (i < list.size() - 1) {
					buf.append(",");
				}
			}
			buf.append("],size:");
			buf.append(maxSize);
			ret = MarketUtils.getResJson(buf.toString());
		} catch (Exception ex) {
			ex.printStackTrace();
			ret = MarketUtils.getThrowJson();
		}
		return ret;
	}

	/**
	 * 分类信息-详细软件列表[type=7]
	 */
	public String initTypeInfo(HttpServletRequest request) {
		String ret = null;
		try {
			int size = Integer.parseInt(request.getParameter("pageSize"));
			int no = Integer.parseInt(request.getParameter("pageNo"));
			int id = Integer.parseInt(request.getParameter("typeId"));
			List<Software> list = null;
			int maxSize = 0;
			list = softwareDao.getAllTypeInfo(size, no, id);
			if (no == 1) {
				maxSize = softwareDao.getTypeInfoCount(id);
			}
			StringBuffer buf = new StringBuffer();
			buf.append("[");
			for (int i = 0; i < list.size(); i++) {
				Software software = list.get(i);
				buf.append("{");
				buf.append("id:");
				buf.append(software.getId());
				buf.append(",name:\"");
				buf.append(JSONTools.string2Json(software.getName()));
				buf.append("\",icon:\"");
				buf.append(software.getIcon());
				buf.append("\",develope:\"");
				buf.append(JSONTools.string2Json(software.getDevelope()));
				buf.append("\",avg:");
				buf.append(software.getAvg());
				buf.append("}");
				if (i < list.size() - 1) {
					buf.append(",");
				}
			}
			buf.append("],size:");
			buf.append(maxSize);
			ret = MarketUtils.getResJson(buf.toString());
		} catch (Exception ex) {
			ex.printStackTrace();
			ret = MarketUtils.getThrowJson();
		}
		return ret;
	}

	/**
	 * 专题信息-详细软件列表[type=13]
	 */
	public String initResourcesInfo(HttpServletRequest request) {
		String ret = null;
		try {
			int size = Integer.parseInt(request.getParameter("pageSize"));
			int no = Integer.parseInt(request.getParameter("pageNo"));
			int id = Integer.parseInt(request.getParameter("resId"));
			List<Software> list = null;
			int maxSize = 0;
			list = softwareDao.getAllResourcesInfo(size, no, id);
			if (no == 1) {
				maxSize = softwareDao.getResourcesInfoCount(id);
			}
			StringBuffer buf = new StringBuffer();
			buf.append("[");
			for (int i = 0; i < list.size(); i++) {
				Software software = list.get(i);
				buf.append("{");
				buf.append("id:");
				buf.append(software.getId());
				buf.append(",name:\"");
				buf.append(JSONTools.string2Json(software.getName()));
				buf.append("\",icon:\"");
				buf.append(software.getIcon());
				buf.append("\",develope:\"");
				buf.append(JSONTools.string2Json(software.getDevelope()));
				buf.append("\",avg:");
				buf.append(software.getAvg());
				buf.append("}");
				if (i < list.size() - 1) {
					buf.append(",");
				}
			}
			buf.append("],size:");
			buf.append(maxSize);
			ret = MarketUtils.getResJson(buf.toString());
		} catch (Exception ex) {
			ex.printStackTrace();
			ret = MarketUtils.getThrowJson();
		}
		return ret;
	}
	private Random random = new Random();
	/**
	 * 软件详细信息[type=8]
	 */
	public String getSoftware(HttpServletRequest request) {
		String ret = null;
		try {
			String softId = request.getParameter("softId");
			StringBuffer buf = new StringBuffer();
			Software software = (Software) softwareDao.getById(Software.class,
					Integer.parseInt(softId));
			int comment = commentDao.getCountBySoftId(Integer.parseInt(softId));
			buf.append("{");
			buf.append("id:");
			buf.append(software.getId());
			buf.append(",name:\"");
			buf.append(JSONTools.string2Json(software.getName()));
			buf.append("\",icon:\"");
			buf.append(software.getIcon());
			buf.append("\",version:\"");
			buf.append(software.getVersion());
			buf.append("\",sdk:");
			buf.append(software.getSdk());
			buf.append(",avg:");
			buf.append(software.getAvg());
			buf.append(",package:\"");
			buf.append(software.getPackage_());
			buf.append("\",time:\"");
			buf.append(DateUtils.timestampToString(software.getTime()));
			buf.append("\",size:\"");
			buf.append(software.getSize());
			buf.append("\",remark:\"");
			buf.append(JSONTools.string2Json(software.getRemark()));
			buf.append("\",res:\"");
			buf.append(JSONTools.string2Json(software.getRes()));
			buf.append("\",count:");
			buf.append(software.getCount()+random.nextInt(10000));
			buf.append(",comment:");
			buf.append(comment);
			buf.append("}");
			ret = MarketUtils.getResJson(buf.toString());
		} catch (Exception ex) {
			ex.printStackTrace();
			ret = MarketUtils.getThrowJson();
		}
		return ret;
	}
}
